package com.banzhi.shop.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.banzhi.shop.MyApp;
import com.banzhi.shop.R;
import com.banzhi.shop.adapter.CommonHeadAdapter;
import com.banzhi.shop.base.BaseActivity;
import com.banzhi.shop.entity.Group.GroupShop;
import com.banzhi.shop.model.CallBack.ManagementCallBack;
import com.banzhi.shop.model.IGroupShop;
import com.banzhi.shop.model.Impl.GroupShopImpl;
import com.banzhi.shop.utils.ASUtils;
import com.banzhi.shop.utils.EndlessRecyclerOnScrollListener;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.NoTokenUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.banzhi.shop.utils.Translucent;
import com.squareup.picasso.Picasso;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 品牌团
 */
public class GroupShopActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener, ManagementCallBack {
    private ImageView mImgBack; // 退出
    private SwipeRefreshLayout mRefreshLayout; // 下拉
    private RelativeLayout mEntityData; // 空数据布局
    private RecyclerView mRecyclerView; // 控件
    private List<GroupShop> mData = new ArrayList<>(); // 数据集合
    private IGroupShop model;
    private int page = 1; // 页码
    private CommonHeadAdapter<GroupShop> adapter; // 适配器
    private LinearLayoutManager layoutManager; // 管理器

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_shop);
        Translucent.setTranslucent(this);
        initView();
        initListener();
        model = new GroupShopImpl();
        LoadingDialogUtils.showProgress(this, "正在加载，请稍后");
        model.getGroup("1", 1, this);
    }

    private void initListener() {
        mImgBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mRefreshLayout.setOnRefreshListener(this);
        mRecyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener(layoutManager) {
            @Override
            public void onLoadMore() {
                page++;
                LoadingDialogUtils.showProgress(GroupShopActivity.this, "正在加载,请稍后~");
                model.getGroup("1", page, GroupShopActivity.this);
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    private void initView() {
        mImgBack = (ImageView) findViewById(R.id.iv_back);
        mRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.srlayout);
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        mEntityData = (RelativeLayout) findViewById(R.id.rl_nodata);
        mRefreshLayout.setColorSchemeResources(R.color.themeColor);
        mRefreshLayout.setProgressBackgroundColorSchemeResource(R.color.colorWhite);
        layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(layoutManager);
    }

    /**
     * 下拉刷新
     */
    @Override
    public void onRefresh() {
        page = 1;
        LoadingDialogUtils.showProgress(this, "正在加载,请稍后~");
        model.getGroup("1", page, this);
    }

    /**
     * 展示列表数据
     */
    private void showView() {
        if (adapter == null) {
            adapter = new CommonHeadAdapter<GroupShop>(this, R.layout.item_group_shop, mData) {
                @Override
                public void convert(ViewHolder holder, GroupShop data, int position) {
                    if (ASUtils.isCanUse(data.getCover())) {
                        Picasso.with(MyApp.context).load(data.getCover()).placeholder(R.mipmap.vp_zhanweitu).into(holder.getImageView(R.id.item_group_cover));
                    }
                    holder.setText(R.id.item_group_name, data.getName());
                    holder.setText(R.id.item_group_detail, data.getDetail());
                    if (ASUtils.isCanUse(data.getLogo())) {
                        Picasso.with(MyApp.context).load(data.getLogo()).placeholder(R.mipmap.zhanweitu2).into(holder.getImageView(R.id.item_group_logo));
                    }
                }
                @Override
                public void headconvert(HeaderViewHolder holder) {

                }
            };
            mRecyclerView.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }
        adapter.setOnItemClickListener(new CommonHeadAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(GroupShopActivity.this, GroupShopDetailActivity.class);
                intent.putExtra("brandId", mData.get(position).getId());
                intent.putExtra("title",mData.get(position).getName());
                startActivity(intent);
            }
        });

    }

    /**
     * 获取数据成功
     */
    @Override
    public void ongetDataSuccess(Object object) {
        if (mRefreshLayout.isRefreshing()) {
            mRefreshLayout.setRefreshing(false);
        }
        if (page == 1) {
            mData.clear();
        }
        mData.addAll((List<GroupShop>) object);
        if (mData.size() == 0) {
            mEntityData.setVisibility(View.VISIBLE);
        } else {
            mEntityData.setVisibility(View.GONE);
        }
        showView();
        LoadingDialogUtils.dismiss();
    }

    /**
     * 获取数据失败
     */
    @Override
    public void ongetDataError(Object object) {
        if (mRefreshLayout.isRefreshing()) {
            mRefreshLayout.setRefreshing(false);
        }
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(GroupShopActivity.this, jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
    }

    @Override
    public void deleteGoodsSuccess(Object object) {

    }

    @Override
    public void deleteGoodsError(Object object) {

    }
}
